%% Aim
% Perform advanced statistical analyses on aDDM outputs with first gaze

%% Setup
clc;
clear;
root_path = pwd; 
addpath(genpath(root_path));

nrole=2;
nblock=2;
ntrial=100;
nprice=10;
nlottery=10;

nchoice=2;

role_label={'Buyer','Seller','Contrast'};
role_color_dark={[235/255 112/255 99/255],[106/255 142/255 208/255]}; 
role_color_median={[238/255 137/255 126/255],[144/255 171/255 220/255],[0.75 0.75 0.75]}; 
role_color_light={[243/255 171/255 163/255],[165/255 187/255 227/255]};

attribute_label={'Price','Lottery'};
attribute_color={[91/255,155/255,213/255],[112/255,173/255,71/255],[166/255,166/255,166/255]}; % Price vs. Lottery

bias_label={'Val','Res','Val vs. Res'};
bias_color_median={[195/255 205/255 147/255],[192/255 173/255 203/255]}; 

choice_label={'Accept','Reject'};
choice_color={[192/256 0/256 0/256],[0/256 32/256 102/256]};
choice_color_light={[255/256 109/256 109/256],[133/256 171/256 255/256]};

%% Load data
sub_data=readtable([root_path,'\data\subject\subject.csv']);
sub_beh_data=readtable([root_path,'\data\subject\subject_behavior.csv']);
sub_ddm_data=readtable([root_path,'\data\subject\subject_ddm.csv']);
sub_gaze_data=readtable([root_path,'\data\subject\subject_gaze.csv']);
beh_data=readtable([root_path,'\data\behavior\behavior.csv']);
gaze_trial_data=readtable([root_path,'\data\gaze\gaze_trial.csv']);
eye_trial_data=readtable([root_path,'\data\eye\eye_trial.csv']);
nsub=height(sub_data);

%% Concatenated data
sub_data = [sub_data, sub_gaze_data(:,4:end)];
trial_data = [beh_data, eye_trial_data(:,9:end), gaze_trial_data(:,9:end)];

%%










%% aDDM4ac: first gaze effect on a by role

raw_data=readtable([root_path,'\model\hddm\aDDM4ac\aDDM4ac_trace.csv']);

addm_trace_data=table;
addm_trace_data.t=raw_data.t;
addm_trace_data.aBuy=raw_data.a_Intercept;
addm_trace_data.aDelta=raw_data.a_C_Role__T_2_;
addm_trace_data.aSell=raw_data.a_Intercept+raw_data.a_C_Role__T_2_;
addm_trace_data.vInterceptBuy=raw_data.v_Intercept;
addm_trace_data.vInterceptDelta=raw_data.v_C_Role__T_2_;
addm_trace_data.vInterceptSell=raw_data.v_Intercept + raw_data.v_C_Role__T_2_;
addm_trace_data.vPrice=raw_data.v_Price;
addm_trace_data.vLotteryBuyGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__1_;
addm_trace_data.vLotteryBuyGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__1_;
addm_trace_data.vLotterySellGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__2_;
addm_trace_data.vLotterySellGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__2_;
addm_trace_data.wLotteryBuyGazeOnPrice   =addm_trace_data.vLotteryBuyGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotteryBuyGazeOnLottery =addm_trace_data.vLotteryBuyGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnPrice  =addm_trace_data.vLotterySellGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnLottery=addm_trace_data.vLotterySellGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnPrice   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnPriceBuyOnLottery   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.ValBiasSellOnLotteryBuyOnPrice   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnLotteryBuyOnLottery   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.zBuy=exp(raw_data.z_Intercept_trans)./(1+exp(raw_data.z_Intercept_trans));
addm_trace_data.zDelta=raw_data.z_C_Role__T_2_;
addm_trace_data.zSell=addm_trace_data.zBuy+addm_trace_data.zDelta;
addm_trace_data.res=addm_trace_data.zSell - addm_trace_data.zBuy;
addm_trace_data.vInterceptBias=addm_trace_data.vInterceptSell - addm_trace_data.vInterceptBuy;
addm_trace_data.aBias=addm_trace_data.aSell - addm_trace_data.aBuy;
addm_trace_data.ratioBuy=(addm_trace_data.wLotteryBuyGazeOnPrice./addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ratioSell=(addm_trace_data.wLotterySellGazeOnPrice./addm_trace_data.wLotterySellGazeOnLottery);

addm_trace_data.zFirstGazeBuy(:)= 0;
addm_trace_data.zFirstGazeSell(:)=0;
addm_trace_data.aFirstGazeBuy(:)= raw_data.a_C_FirstGazeLottery__T_1_;
addm_trace_data.aFirstGazeSell(:)=raw_data.a_C_FirstGazeLottery__T_1_;

addm_trace_data.ValBiasSellOnPriceBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ValBiasSellOnLotteryBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnLotteryBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery);

writetable(addm_trace_data,[root_path,'\model\hddm\aDDM4ac\aDDM4ac_trace_group.csv']);

% mean and 95% credible intervals
A=mean(table2array(addm_trace_data));
B=prctile(table2array(addm_trace_data),2.5);
C=prctile(table2array(addm_trace_data),97.5);
rowNames = {'mean','2.5p','97.5p'};
colNames = {'t','aBuy','aDelta','aSell','vInterceptBuy','vInterceptDelta','vInterceptSell','vPrice',...
            'vLotteryBuyGazeOnPrice','vLotteryBuyGazeOnLottery','vLotterySellGazeOnPrice','vLotterySellGazeOnLottery',...
            'wLotteryBuyGazeOnPrice','wLotteryBuyGazeOnLottery','wLotterySellGazeOnPrice','wLotterySellGazeOnLottery',...
            'ValBiasSellOnPriceBuyOnPrice','ValBiasSellOnPriceBuyOnLottery','ValBiasSellOnLotteryBuyOnPrice','ValBiasSellOnLotteryBuyOnLottery',...
            'zBuy','zDelta','zSell',...
            'ResBias','vInterBias','aBias',...
            'ratioBuy','ratioSell'...
            'zFirstGazeBuy','zFirstGazeSell','aFirstGazeBuy','aFirstGazeSell',...
            'ValBiasSellOnPriceBuyOnPriceLog','ValBiasSellOnPriceBuyOnLotteryLog','ValBiasSellOnLotteryBuyOnPriceLog','ValBiasSellOnLotteryBuyOnLotteryLog'};
addm.group.post.describe = array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames);
disp(addm.group.post.describe);

% likelihood of difference between buyer and seller
addm.group.likelihood.ratioBuy=mean(addm_trace_data.ratioBuy < 1);
addm.group.likelihood.ratioSell=mean(addm_trace_data.ratioSell < 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnLottery=mean(addm_trace_data.ValBiasSellOnPriceBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnPrice=mean(addm_trace_data.ValBiasSellOnPriceBuyOnPrice > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnLottery=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnPrice=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnPrice > 1);
addm.group.likelihood.res=mean(addm_trace_data.zSell > addm_trace_data.zBuy);
addm.group.likelihood.a=mean(addm_trace_data.aSell > addm_trace_data.aBuy);
addm.group.likelihood.vIntercept=mean(addm_trace_data.vInterceptDelta > 0);
disp(addm.group.likelihood);

mean(addm_trace_data.aFirstGazeBuy>0);
mean(addm_trace_data.aFirstGazeSell>0);

%%









%% aDDM4aca: first gaze effect on a by role

raw_data=readtable([root_path,'\model\hddm\aDDM4aca\aDDM4aca_trace.csv']);

addm_trace_data=table;
addm_trace_data.t=raw_data.t;
addm_trace_data.aBuy=raw_data.a_Intercept;
addm_trace_data.aDelta=raw_data.a_C_Role__T_2_;
addm_trace_data.aSell=raw_data.a_Intercept+raw_data.a_C_Role__T_2_;
addm_trace_data.vInterceptBuy=raw_data.v_Intercept;
addm_trace_data.vInterceptDelta=raw_data.v_C_Role__T_2_;
addm_trace_data.vInterceptSell=raw_data.v_Intercept + raw_data.v_C_Role__T_2_;
addm_trace_data.vPrice=raw_data.v_Price;
addm_trace_data.vLotteryBuyGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__1_;
addm_trace_data.vLotteryBuyGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__1_;
addm_trace_data.vLotterySellGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__2_;
addm_trace_data.vLotterySellGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__2_;
addm_trace_data.wLotteryBuyGazeOnPrice   =addm_trace_data.vLotteryBuyGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotteryBuyGazeOnLottery =addm_trace_data.vLotteryBuyGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnPrice  =addm_trace_data.vLotterySellGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnLottery=addm_trace_data.vLotterySellGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnPrice   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnPriceBuyOnLottery   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.ValBiasSellOnLotteryBuyOnPrice   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnLotteryBuyOnLottery   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.zBuy=exp(raw_data.z_Intercept_trans)./(1+exp(raw_data.z_Intercept_trans));
addm_trace_data.zDelta=raw_data.z_C_Role__T_2_;
addm_trace_data.zSell=addm_trace_data.zBuy+addm_trace_data.zDelta;
addm_trace_data.res=addm_trace_data.zSell - addm_trace_data.zBuy;
addm_trace_data.vInterceptBias=addm_trace_data.vInterceptSell - addm_trace_data.vInterceptBuy;
addm_trace_data.aBias=addm_trace_data.aSell - addm_trace_data.aBuy;
addm_trace_data.ratioBuy=(addm_trace_data.wLotteryBuyGazeOnPrice./addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ratioSell=(addm_trace_data.wLotterySellGazeOnPrice./addm_trace_data.wLotterySellGazeOnLottery);

addm_trace_data.zFirstGazeBuy(:)= 0;
addm_trace_data.zFirstGazeSell(:)=0;
addm_trace_data.aFirstGazeBuy(:)= raw_data.a_C_FirstGazeLottery__T_1__C_Role__1_;
addm_trace_data.aFirstGazeSell(:)=raw_data.a_C_FirstGazeLottery__T_1__C_Role__2_;

addm_trace_data.ValBiasSellOnPriceBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ValBiasSellOnLotteryBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnLotteryBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery);

writetable(addm_trace_data,[root_path,'\model\hddm\aDDM4aca\aDDM4aca_trace_group.csv']);

% mean and 95% credible intervals
A=mean(table2array(addm_trace_data));
B=prctile(table2array(addm_trace_data),2.5);
C=prctile(table2array(addm_trace_data),97.5);
rowNames = {'mean','2.5p','97.5p'};
colNames = {'t','aBuy','aDelta','aSell','vInterceptBuy','vInterceptDelta','vInterceptSell','vPrice',...
            'vLotteryBuyGazeOnPrice','vLotteryBuyGazeOnLottery','vLotterySellGazeOnPrice','vLotterySellGazeOnLottery',...
            'wLotteryBuyGazeOnPrice','wLotteryBuyGazeOnLottery','wLotterySellGazeOnPrice','wLotterySellGazeOnLottery',...
            'ValBiasSellOnPriceBuyOnPrice','ValBiasSellOnPriceBuyOnLottery','ValBiasSellOnLotteryBuyOnPrice','ValBiasSellOnLotteryBuyOnLottery',...
            'zBuy','zDelta','zSell',...
            'ResBias','vInterBias','aBias',...
            'ratioBuy','ratioSell'...
            'zFirstGazeBuy','zFirstGazeSell','aFirstGazeBuy','aFirstGazeSell',...
            'ValBiasSellOnPriceBuyOnPriceLog','ValBiasSellOnPriceBuyOnLotteryLog','ValBiasSellOnLotteryBuyOnPriceLog','ValBiasSellOnLotteryBuyOnLotteryLog'};
addm.group.post.describe = array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames);
disp(addm.group.post.describe);

% likelihood of difference between buyer and seller
addm.group.likelihood.ratioBuy=mean(addm_trace_data.ratioBuy < 1);
addm.group.likelihood.ratioSell=mean(addm_trace_data.ratioSell < 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnLottery=mean(addm_trace_data.ValBiasSellOnPriceBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnPrice=mean(addm_trace_data.ValBiasSellOnPriceBuyOnPrice > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnLottery=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnPrice=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnPrice > 1);
addm.group.likelihood.res=mean(addm_trace_data.zSell > addm_trace_data.zBuy);
addm.group.likelihood.a=mean(addm_trace_data.aSell > addm_trace_data.aBuy);
addm.group.likelihood.vIntercept=mean(addm_trace_data.vInterceptDelta > 0);
disp(addm.group.likelihood);

mean(addm_trace_data.aFirstGazeBuy>0);
mean(addm_trace_data.aFirstGazeSell>0);

mean(addm_trace_data.aFirstGazeBuy>addm_trace_data.aFirstGazeSell);

mean(addm_trace_data.aFirstGazeBuy-addm_trace_data.aFirstGazeSell);
prctile(addm_trace_data.aFirstGazeBuy-addm_trace_data.aFirstGazeSell, 2.5);
prctile(addm_trace_data.aFirstGazeBuy-addm_trace_data.aFirstGazeSell, 97.5);

%%









%% aDDM4aa: first gaze effect on z by role

raw_data=readtable([root_path,'\model\hddm\aDDM4aa\aDDM4aa_trace.csv']);

addm_trace_data=table;
addm_trace_data.t=raw_data.t;
addm_trace_data.aBuy=raw_data.a_Intercept;
addm_trace_data.aDelta=raw_data.a_C_Role__T_2_;
addm_trace_data.aSell=raw_data.a_Intercept+raw_data.a_C_Role__T_2_;
addm_trace_data.vInterceptBuy=raw_data.v_Intercept;
addm_trace_data.vInterceptDelta=raw_data.v_C_Role__T_2_;
addm_trace_data.vInterceptSell=raw_data.v_Intercept + raw_data.v_C_Role__T_2_;
addm_trace_data.vPrice=raw_data.v_Price;
addm_trace_data.vLotteryBuyGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__1_;
addm_trace_data.vLotteryBuyGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__1_;
addm_trace_data.vLotterySellGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__2_;
addm_trace_data.vLotterySellGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__2_;
addm_trace_data.wLotteryBuyGazeOnPrice   =addm_trace_data.vLotteryBuyGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotteryBuyGazeOnLottery =addm_trace_data.vLotteryBuyGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnPrice  =addm_trace_data.vLotterySellGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnLottery=addm_trace_data.vLotterySellGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnPrice   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnPriceBuyOnLottery   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.ValBiasSellOnLotteryBuyOnPrice   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnLotteryBuyOnLottery   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.zBuy=exp(raw_data.z_Intercept_trans)./(1+exp(raw_data.z_Intercept_trans));
addm_trace_data.zDelta=raw_data.z_C_Role__T_2_;
addm_trace_data.zSell=addm_trace_data.zBuy+addm_trace_data.zDelta;
addm_trace_data.res=addm_trace_data.zSell - addm_trace_data.zBuy;
addm_trace_data.vInterceptBias=addm_trace_data.vInterceptSell - addm_trace_data.vInterceptBuy;
addm_trace_data.aBias=addm_trace_data.aSell - addm_trace_data.aBuy;
addm_trace_data.ratioBuy=(addm_trace_data.wLotteryBuyGazeOnPrice./addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ratioSell=(addm_trace_data.wLotterySellGazeOnPrice./addm_trace_data.wLotterySellGazeOnLottery);

addm_trace_data.zFirstGazeBuy= raw_data.z_C_FirstGazeLottery__T_1_;
addm_trace_data.zFirstGazeSell=raw_data.z_C_FirstGazeLottery__T_1_;
addm_trace_data.aFirstGazeBuy(:)= 0;
addm_trace_data.aFirstGazeSell(:)=0;

addm_trace_data.ValBiasSellOnPriceBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ValBiasSellOnLotteryBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnLotteryBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery);

writetable(addm_trace_data,[root_path,'\model\hddm\aDDM4aa\aDDM4aa_trace_group.csv']);

% mean and 95% credible intervals
A=mean(table2array(addm_trace_data));
B=prctile(table2array(addm_trace_data),2.5);
C=prctile(table2array(addm_trace_data),97.5);
rowNames = {'mean','2.5p','97.5p'};
colNames = {'t','aBuy','aDelta','aSell','vInterceptBuy','vInterceptDelta','vInterceptSell','vPrice',...
            'vLotteryBuyGazeOnPrice','vLotteryBuyGazeOnLottery','vLotterySellGazeOnPrice','vLotterySellGazeOnLottery',...
            'wLotteryBuyGazeOnPrice','wLotteryBuyGazeOnLottery','wLotterySellGazeOnPrice','wLotterySellGazeOnLottery',...
            'ValBiasSellOnPriceBuyOnPrice','ValBiasSellOnPriceBuyOnLottery','ValBiasSellOnLotteryBuyOnPrice','ValBiasSellOnLotteryBuyOnLottery',...
            'zBuy','zDelta','zSell',...
            'ResBias','vInterBias','aBias',...
            'ratioBuy','ratioSell'...
            'zFirstGazeBuy','zFirstGazeSell','aFirstGazeBuy','aFirstGazeSell',...
            'ValBiasSellOnPriceBuyOnPriceLog','ValBiasSellOnPriceBuyOnLotteryLog','ValBiasSellOnLotteryBuyOnPriceLog','ValBiasSellOnLotteryBuyOnLotteryLog'};
addm.group.post.describe = array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames);
disp(addm.group.post.describe);

% likelihood of difference between buyer and seller
addm.group.likelihood.ratioBuy=mean(addm_trace_data.ratioBuy < 1);
addm.group.likelihood.ratioSell=mean(addm_trace_data.ratioSell < 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnLottery=mean(addm_trace_data.ValBiasSellOnPriceBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnPrice=mean(addm_trace_data.ValBiasSellOnPriceBuyOnPrice > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnLottery=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnPrice=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnPrice > 1);
addm.group.likelihood.res=mean(addm_trace_data.zSell > addm_trace_data.zBuy);
addm.group.likelihood.a=mean(addm_trace_data.aSell > addm_trace_data.aBuy);
addm.group.likelihood.vIntercept=mean(addm_trace_data.vInterceptDelta > 0);
disp(addm.group.likelihood);

mean(addm_trace_data.zFirstGazeBuy>0);
mean(addm_trace_data.zFirstGazeSell>0);

%%










%% aDDM4aaa: first gaze effect on z by role

raw_data=readtable([root_path,'\model\hddm\aDDM4aaa\aDDM4aaa_trace.csv']);

addm_trace_data=table;
addm_trace_data.t=raw_data.t;
addm_trace_data.aBuy=raw_data.a_Intercept;
addm_trace_data.aDelta=raw_data.a_C_Role__T_2_;
addm_trace_data.aSell=raw_data.a_Intercept+raw_data.a_C_Role__T_2_;
addm_trace_data.vInterceptBuy=raw_data.v_Intercept;
addm_trace_data.vInterceptDelta=raw_data.v_C_Role__T_2_;
addm_trace_data.vInterceptSell=raw_data.v_Intercept + raw_data.v_C_Role__T_2_;
addm_trace_data.vPrice=raw_data.v_Price;
addm_trace_data.vLotteryBuyGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__1_;
addm_trace_data.vLotteryBuyGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__1_;
addm_trace_data.vLotterySellGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__2_;
addm_trace_data.vLotterySellGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__2_;
addm_trace_data.wLotteryBuyGazeOnPrice   =addm_trace_data.vLotteryBuyGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotteryBuyGazeOnLottery =addm_trace_data.vLotteryBuyGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnPrice  =addm_trace_data.vLotterySellGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnLottery=addm_trace_data.vLotterySellGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnPrice   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnPriceBuyOnLottery   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.ValBiasSellOnLotteryBuyOnPrice   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnLotteryBuyOnLottery   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.zBuy=exp(raw_data.z_Intercept_trans)./(1+exp(raw_data.z_Intercept_trans));
addm_trace_data.zDelta=raw_data.z_C_Role__T_2_;
addm_trace_data.zSell=addm_trace_data.zBuy+addm_trace_data.zDelta;
addm_trace_data.res=addm_trace_data.zSell - addm_trace_data.zBuy;
addm_trace_data.vInterceptBias=addm_trace_data.vInterceptSell - addm_trace_data.vInterceptBuy;
addm_trace_data.aBias=addm_trace_data.aSell - addm_trace_data.aBuy;
addm_trace_data.ratioBuy=(addm_trace_data.wLotteryBuyGazeOnPrice./addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ratioSell=(addm_trace_data.wLotterySellGazeOnPrice./addm_trace_data.wLotterySellGazeOnLottery);

addm_trace_data.zFirstGazeBuy= raw_data.z_C_FirstGazeLottery__T_1__C_Role__1_;
addm_trace_data.zFirstGazeSell=raw_data.z_C_FirstGazeLottery__T_1__C_Role__2_;
addm_trace_data.aFirstGazeBuy(:)= 0;
addm_trace_data.aFirstGazeSell(:)=0;

addm_trace_data.ValBiasSellOnPriceBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ValBiasSellOnLotteryBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnLotteryBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery);

writetable(addm_trace_data,[root_path,'\model\hddm\aDDM4aaa\aDDM4aaa_trace_group.csv']);

% mean and 95% credible intervals
A=mean(table2array(addm_trace_data));
B=prctile(table2array(addm_trace_data),2.5);
C=prctile(table2array(addm_trace_data),97.5);
rowNames = {'mean','2.5p','97.5p'};
colNames = {'t','aBuy','aDelta','aSell','vInterceptBuy','vInterceptDelta','vInterceptSell','vPrice',...
            'vLotteryBuyGazeOnPrice','vLotteryBuyGazeOnLottery','vLotterySellGazeOnPrice','vLotterySellGazeOnLottery',...
            'wLotteryBuyGazeOnPrice','wLotteryBuyGazeOnLottery','wLotterySellGazeOnPrice','wLotterySellGazeOnLottery',...
            'ValBiasSellOnPriceBuyOnPrice','ValBiasSellOnPriceBuyOnLottery','ValBiasSellOnLotteryBuyOnPrice','ValBiasSellOnLotteryBuyOnLottery',...
            'zBuy','zDelta','zSell',...
            'ResBias','vInterBias','aBias',...
            'ratioBuy','ratioSell'...
            'zFirstGazeBuy','zFirstGazeSell','aFirstGazeBuy','aFirstGazeSell',...
            'ValBiasSellOnPriceBuyOnPriceLog','ValBiasSellOnPriceBuyOnLotteryLog','ValBiasSellOnLotteryBuyOnPriceLog','ValBiasSellOnLotteryBuyOnLotteryLog'};
addm.group.post.describe = array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames);
disp(addm.group.post.describe);

% likelihood of difference between buyer and seller
addm.group.likelihood.ratioBuy=mean(addm_trace_data.ratioBuy < 1);
addm.group.likelihood.ratioSell=mean(addm_trace_data.ratioSell < 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnLottery=mean(addm_trace_data.ValBiasSellOnPriceBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnPrice=mean(addm_trace_data.ValBiasSellOnPriceBuyOnPrice > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnLottery=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnPrice=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnPrice > 1);
addm.group.likelihood.res=mean(addm_trace_data.zSell > addm_trace_data.zBuy);
addm.group.likelihood.a=mean(addm_trace_data.aSell > addm_trace_data.aBuy);
addm.group.likelihood.vIntercept=mean(addm_trace_data.vInterceptDelta > 0);
disp(addm.group.likelihood);

mean(addm_trace_data.zFirstGazeBuy>0);
mean(addm_trace_data.zFirstGazeSell>0);

%% Subject posteriors identified from ***post_subject.csv

ddm_post_data=readtable([root_path,'\model\hddm\aDDM4aaa\aDDM4aaa_post_subject.csv']);

sub_data.zFirstGazeBuy=ddm_post_data.zFirstGazeBuy;
sub_data.zFirstGazeSell=ddm_post_data.zFirstGazeSell;
sub_data.zFirstGazeBias=sub_data.zFirstGazeSell - sub_data.zFirstGazeBuy;

[r,p]=corr(sub_data.zFirstGazeBias, sub_data.FirstGazeLotteryRatioBiasMinus);
[r,p]=corr(sub_data.zFirstGazeBias, sub_data.ResBias);


[r,p]=corr(sub_data.ResBias,ddm_post_data.zDelta);

%%






%% DIC
dic_raw=readtable([root_path,'\model\hddm\aDDM4ac\aDDM4ac_dic.csv']);
dic.aDDM4ac=dic_raw.dic(1);
dic_raw=readtable([root_path,'\model\hddm\aDDM4aca\aDDM4aca_dic.csv']);
dic.aDDM4aca=dic_raw.dic(1);
dic_raw=readtable([root_path,'\model\hddm\aDDM4aa\aDDM4aa_dic.csv']);
dic.aDDM4aa=dic_raw.dic(1);
dic_raw=readtable([root_path,'\model\hddm\aDDM4aaa\aDDM4aaa_dic.csv']);
dic.aDDM4aaa=dic_raw.dic(1);

%%